/**
 * 接收录音设备的录音文件必须的2个接口。
 */
package com.siasun.vaemrd.controller;

import com.alibaba.fastjson2.JSONObject;
import com.siasun.vaemrd.entity.TokenDto;
import com.siasun.vaemrd.service.AztService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;


@Slf4j
@RestController
public class AztController {

    private final AztService aztService;

    @Autowired
    public AztController(AztService aztService) {
        this.aztService = aztService;
    }



    /**
     * token获取接口
     * @param dto {"appId":"SmartQuality","mac":"84:b4:d2:62:42:c8","secret":"IqcServerRongDa888","xtime":"20250407165619"}
     * @param request 可选参数，正式版本移除.
     * @return json
     * {"returnCode":"0000","returnMessage":"success","bean":{"accessToken":"1680625069192420566","expiresIn":"7200"}}
     */
    @PostMapping(value="/api/sso/getToken/",produces = "application/json")
    public ResponseEntity<Map<String, Object>> getToken(@RequestBody TokenDto dto,
                                                        HttpServletRequest request) {
        RestfulControllerHelper.logRequestContext(request, log);
        log.info("Request Body: {}", dto.toString());

        return new ResponseEntity<>(this.aztService.getToken(dto), HttpStatus.OK);
    }

    /**
     * 豚音录音上传接口
     *
     * @param file          文件
     * @param appId         鉴权appId
     * @param transactionId 流水号
     * @param accessToken   token
     * @param recPath       文件上传路径
     * @param xtime         时间戳
     * @param signature     加密密文
     * @param mac           设备mac
     * @return json
     */
    @PostMapping(value = "/api/sso/uploadRec",produces="multipart/form-data")
    public ResponseEntity<Map<String, Object>> uploadRec(@RequestParam("file") MultipartFile file,
                                                         @RequestParam("appId") String appId,
                                                         @RequestParam("transactionId") String transactionId,
                                                         @RequestParam("accessToken") String accessToken,
                                                         @RequestParam("recPath") String recPath,
                                                         @RequestParam("xtime") String xtime,
                                                         @RequestParam("signature") String signature,
                                                         @RequestParam("mac") String mac,
                                                         @RequestParam("audioPath") String audioPath,
                                                         HttpServletRequest request) {
        RestfulControllerHelper.logRequestContext(request, log);
        log.info("uploadRec request args=>appId: {},transactionId:{},accessToken:{},recPath:{},xtime:{},signature:{},mac:{},audioPath:{}",
                appId, transactionId, accessToken, recPath, xtime, signature, mac, audioPath);
        JSONObject response= aztService.uploadRec(file,
                appId, transactionId, accessToken, recPath, xtime, signature, mac, audioPath);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

}

